//
//  NewsViewController.m
//  Dispatch
//
//  Created by hyeyeon on 14. 2. 12..
//  Copyright (c) 2014년 hyeyeon. All rights reserved.
//

#import "NewsViewController.h"

@interface NewsViewController ()

@end

@implementation NewsViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [self makeObject];
        
        activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        [self.view addSubview:activityView];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    self.view.backgroundColor = ColorFromRGB(0xe9e9e9);

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 생성
-(void)makeObject{
    
    
    mWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, myOrginY, mySize.width, mySize.height-20)];
    mWebView.backgroundColor = ColorFromRGB(0xffffff);
    [mWebView setScalesPageToFit:YES];
    mWebView.delegate = self;
    mWebView.scrollView.delegate = self;
    [self.view addSubview:mWebView];
    
    float gap = deviceIOS7 ? 0 : 20;
    UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, mySize.height-51-gap, mySize.width, 51)];
    bottomView.alpha = 0.95f;
    bottomView.backgroundColor = ColorFromRGB(0x211f20);
    [self.view addSubview:bottomView];
    
    UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 0, 41, 51)];
    [backButton setImage:[UIImage imageNamed:@"btn_back03"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:backButton];
    
    UIButton *topButton = [[UIButton alloc] initWithFrame:CGRectMake(bottomView.frame.size.width-10-41, 0, 41, 51)];
    [topButton setImage:[UIImage imageNamed:@"btn_top02"] forState:UIControlStateNormal];
    [topButton addTarget:self action:@selector(topButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:topButton];
    
    
}

#pragma mark - 뒤로
-(void)backButtonAction:(id)sender{
    [self dismissModalViewControllerAnimated:YES];

}

#pragma mark - 위로
-(void)topButtonAction:(id)sender{
    
    [mWebView.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
}

#pragma mark - 샛팅
-(void)setNewsTitle:(NSString*)title andURL:(NSURL*)url{

    [mWebView loadRequest:[NSURLRequest requestWithURL:url]];
}

#pragma mark - UIWebView Delegate
-(void)webViewDidStartLoad:(UIWebView *)webView{
    [activityView startAnimating];
}

-(void)webViewDidFinishLoad:(UIWebView *)webView{
    currentContentHeight = webView.scrollView.contentSize.height;
    [activityView stopAnimating];
}

-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    [activityView stopAnimating];
}

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    return YES;
}

#pragma mark -  scroll delegate

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
}


- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    if (!decelerate) {
        
        [mWebView.scrollView setContentSize:CGSizeMake(mWebView.frame.size.width, currentContentHeight+51)];
    }
}

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    
    [mWebView.scrollView setContentSize:CGSizeMake(mWebView.frame.size.width, currentContentHeight+51)];
}

@end
